package com.team.childapp.http

import android.content.Context
import android.text.TextUtils
import com.lzy.okgo.model.Response
import com.team.childapp.R
import com.team.childapp.base.listener.ICallBackResultListener
import com.team.childapp.bean.study.StudyGardenBean
import com.team.childapp.config.Api
import com.team.childapp.config.RequestID
import com.team.childapp.http.base.AppBaseLogical
import com.team.childapp.http.base.BaseReqBean
import com.team.childapp.http.base.LoadingBaseObserver
import com.team.childapp.util.JsonSerializer
import com.team.childapp.util.LogUtils
import com.team.childapp.util.ToastUtil

/**
 * Create on: 2025/1/4
 * by Mr.Zhu
 * Description: TODO
 */
class StudyGardenLogical : AppBaseLogical() {

    companion object {
        private val instance: StudyGardenLogical by lazy { StudyGardenLogical() }

        fun getInstanceLogin(): StudyGardenLogical {
            return instance
        }
    }

    fun requestStudyList(context: Context?,pageNum: String?,pageSize: String?,resultListener: ICallBackResultListener?) {
        val url: String = Api.StudyGarden.URL_GET_STUDY
        val reqBean: BaseReqBean = BaseReqBean()
        reqBean.getMap().put("pageNum", pageNum)
        reqBean.getMap().put("pageSize", pageSize)
        requestStringForLogin(
            url,
            reqBean.getMap(),
            CommonObserver(
                context,
                RequestID.StudyGarden.STUDY_LIST,
                resultListener
            )
        )
    }


    private class CommonObserver(activity: Context?,
        private val requestID: Int,
        private val resultListener: ICallBackResultListener?
    ) :LoadingBaseObserver() {
        init {
            setShowLoading(activity)
        }

        override fun onNext(stringResponse: Response<String>) {
            super.onNext(stringResponse)
            if (stringResponse.code() != 200 || TextUtils.isEmpty(stringResponse.body())) {
                ToastUtil.showLong(R.string.data_fail)
                return
            }
            val bodyString = stringResponse.body()
            try {
                when (requestID) {
                    RequestID.StudyGarden.STUDY_LIST -> {
                        LogUtils.showLog("账号登录 ：$bodyString")
                        val bean: StudyGardenBean = JsonSerializer.deSerialize(
                            bodyString,
                            StudyGardenBean::class.java
                        )
                        if (bean.code == 200) {
                            resultListener?.onCallBack(bean)
                        } else {
                            ToastUtil.showLong(bean.msg)
                            resultListener?.onCallBack(null)
                        }
                    }
                }
            } catch (e: Exception) {
                ToastUtil.showLong(R.string.data_exception)
                resultListener?.onCallBack(0)
            }

        }

        override fun onError(e: Throwable) {
            super.onError(e)
            resultListener?.onCallBack(null)

        }
    }
}